### FILE="Main.annotation"
# Copyright:	Public domain.
# Filename:	TIME_OF_FREE-FALL_CALCULATIONS.agc
# Purpose:	Part of the source code for Solarium build 55. This
#		is for the Command Module's (CM) Apollo Guidance
#		Computer (AGC), for Apollo 4.
# Assembler:	yaYUL --block1
# Contact:	Jim Lawton <jim DOT lawton AT gmail DOT com>
# Website:	www.ibiblio.org/apollo/index.html
# Page scans:	www.ibiblio.org/apollo/ScansForConversion/Solarium055/
# Mod history:	2009-10-04 JL	Created.

## Page 682
## (empty page)

## Page 683

# CONIC TIME OF FLIGHT CALCULATION.   PROGRAM CALCULATES THE FREE-FALL TIME OF FLIGHT FROM PRESENT POSITION RN
# AND VELOCITY VN TO A RADIUS SPECIFIED BY RTERM, CORRESPONDING TO 280K OR400K FEET ALTITUDE.
# THE POSITION  RN MAY BE ON EITHER SIDE OF THE ELLIPSE, BUT  RTERM  IS   CONSIDERED ON THE INBOUND SIDE.
#
# THE EQUATION IS	TFF = ( DEL E - (Q2-Q1) ) / (ALFA SQRT( ALFA MUE) )
#
# AND			Q2 = -SQRT( RTERM ALFA (2-RTERM ALFA) -LCP ALFA )	(INBOUND SIDE)		LEQ +/-  LCE
#			     -  -
#			Q1 = RN.VN  SQRT(ALFA/MUE)							LEQ +/-  LCE
#
# PROGRAM REQUIREMENTS ON ENTERING.
#	1.  PUSHDOWN LIST IS ZEROED.
#	2.  INPUTS: POSITION  RN 2(-24) M,  VELOCITY  VN  2(-7)  M/CS
#
# THE PROGRAM EXITS WITH ONE OF THE FOLLOWING IN TFF.
#			A. TFF = FLIGHT TIME.  NORMAL CASE FOR POSITIVE FLIGHT  TIME LESS THAN ONE ORBITAL PERIOD.
#			B. TFF=+0.  THIS INDICATES THAT THE TRAJECTORY HAS GONE PAST RTERM AND THE PRESENT ALTITUDE IS
#			   LESS THAN THE SPECIFIED TERMINAL ALTITUDE.
#			C. TFF = POSMAX.  THIS INDICATES THAT THE ELLIPTICAL CONIC FROM THE PRESENT POSITION WILL NOT
#			   RETURN TO THE SPECIFIED ALTITUDE.
# THE FOLLOWING QUANTITIES REMAIN IN THE PUSHLIST
#	ALFA			2(+21) IN PDL 10D
#	(2-RTERM ALFA)		2(-3)  IN PDL 6
#	RTERM ALFA		2(-3)  IN PDL 4
#	LCP ALFA		2(-6)  IN PDL 2
#
# THUS THE CALLING PROGRAM CAN CALCULATE TERMINAL VELOCITY, VTERM, AND COS GAMMA USING RESIDUE FROM
# CALCTFF.
#
#	     2
#	VTERM  (RTERM/MUE) = (2-RTERM ALFA)
#
#	   2
#	SIN GAMMA = 1 - (LCP ALFA / RTERM ALFA(2-RTERM ALFA)  )
#
#	   2
#	COS GAMMA = LCP ALFA /(RTERM ALFA (2-RTERM ALFA)  )
#
# QUANTITIES ARE NORMALIZED BY LCE TO OBTAIN BEST ACCURACY IN COMPUTING, ESPACIALLY FOR SPARCTAN.  PROGRAM
# ACCEPTS LCE  LEQ 1.0.  THE SMALLEST LCE WHICH IS  NORMALIZED IS 2(-4),  ALTHOUGH THIS MAY BE EXTENDED IF
# DESIRED.  (LCE  L  2(-3) ALL HAVE SAME SCALING)

## Page 684

# TEMPORARY ERASABLE ASSIGNMENTS FOR TFF COMPUTATIONS			PUSHLIST 00 - 19D, X1

RTERM		=	14D

		BANK	31
CALCTFF		EXIT	0

		CCS	REFSWTCH	# SEE IF GROUND DESIRES 280K FF REFERENCE
		TC	+4		# NO
		TC	+3
		CAF	ZERO
		TC	+5
		
		CAF	NOMBURN		# TEST IF ANY NOMINAL BURN (SPS1,SPS2,)
		MASK	FLAGWRD2	# SPS3, SPS4, ARRST FLAG SET
		CCS	A
		CS	TWO
		INDEX	FIXLOC		# X1=-2 FOR 400K FT. AND 0 FOR 280K FT.
		TS	X1
		TC	INTPRET
		DMOVE*	1
		RTB
			R280K,1
			FRESHPD
		STORE	RTERM		#  2(-24)
		
		VSQ	2
		DMP	DMP
		TSLT	BDSU
			VN		# VEL  2 (-7)
			1/MUE		# 2 (+35)
			RMAG		# 2(-25)
			1
			DP1/4		# RMAG ALFA   2 (-3)		TO PDL 0,1
		
		NOLOD	1
		TSRT	DDV
			1
			RMAG
		STORE	10D
		
		NOLOD	1
		BMN
			HIECC
		
		DMP	2
		SQRT	DMP
		TSLC	BDDV
			10D		# ALFA  2(+21)
			MUE(37)
## Page 685
			10D		# ALFA
			19D		# -N. SAVE FOR GETTFF1
			DP2PI/16
		STORE	12D		# ORB PERIOD  2PI/ALFA SQRT(ALFA MUE)
					# 2(-17-N)
		VXV	3
		VSLT	VSQ
		DMP	DMP
		RTB
			RN
			VN		# MTRS/CS   2(-7)
			1
			1/MUE		# 2(+35)
			10D		# ALFA  2(+21)
			NORMBYE		# GET NORM COUNT BASED ON LCE SQ.
					# LCP ALFA  2(-6)		TO PDL 2,3
		
		NOLOD	1
		DSU	BPL
			DP.36
			LOECC
		
		DSU	1
		BMN
			10D
			ALFALIM
			HIECC
			
LOECC		DMP	0
			RTERM		# 2(-24)
			10D		# ALFA  2(+21)
					# RTERM ALFA  2(-3)		TO PDL 4,5
		
		DSU	0
			DP1/4
			4		# RTERM ALFA  2(-3)
					# (2-RTERM ALFA)  2(-3)		TO PDL 6,7
		
		DMP	1
		SQRT
			10D		# ALFA  2(+21)
			1/MUE		# 2(+35)			TO PDL 8,9
		
		DOT	2
		TSLT	DMP
		RTB
			RN
			VN		# 2(-7)
			1
			-		# LEAVE Q1  2(-3) IN MPAC
## Page 686
			ARGQ1		# SAVE  MPAC FOR GONE PAST TEST. , NORMLZE
		STORE	8D		# Q1  (OR Q1/2 OR 2Q1 OR 4Q1)
		
		DMP	2		#  CALCULATE Q2
		DSU	RTB
		DAD
			-		# (2-RTERM ALFA) 2(-3) FROM PDL 6,7
			-		# RTERM ALFA 2(-3) FROM PDL 4,5
			-		# LCP ALFA 2(-3) FROM PDL 2,3
			ARGQ2		# TEST SGN, TAKE ROOT  (RT DOT NEG)
					# RETURN -Q2 (OR -Q2/2 OR -2Q2 OR -4Q2)
			8D		# Q1  (AT SIMILAR SCALE)
		STORE	8D		# Q1-Q2  ( OR /2 OR2 OR4)
					# LEQ +/- 2LCE  (OR 1 OR 4 OR 8)
		
		DSU	1
		RTB	DMP
			6		# 2-RTERM ALFA 2(-3)
			-		# 2-RTERM ALFA-RMAG ALFA  2(-3) IN MPAC
			GETTFF		# C(MPAC) = DEN  2(-3)
			12D		# ORB PERIOD    2(-17-N)
		STORE	TFF
		ITCQ	0
ARGQ1		INDEX	FIXLOC
		INDEX	16D		# COUNT BY ONES
		CAF	BIT4		# EFFECTIVE SH L 3  ( 2 OR 4 OR 5)
		TC	SHORTMP
		XCH	MPAC +2		# MOVE UP
		XCH	MPAC +1
		TS	MPAC		# MPAC IS L .5 AS REQD BY TEST LATER.
		INDEX	FIXLOC
		TS	18D		# SAVE FOR GONE PAST TEST.
		TC	DANZIG
		
ARGQ2		INDEX	FIXLOC
		INDEX	17D		# SH L BY 2,S
		CAF	BIT7		# DO EFFECTIVE SHL 6    (4 OR 8 OR 10)
		TC	SHORTMP		# TP ENTRY , C(MPAC+2) VALID.
		CAF	ZERO
		XCH	MPAC +2
		XCH	MPAC +1
		TS	MPAC
		
		CCS	MPAC
		TC	Q2ROOT
		TC	+2
		TC	NEGQ2		# NO FREEFALL CONIC TO RTERM FROM HERE.
		
		CCS	MPAC +1		# CK LO WORD. IF NEG, ASSUME ROUNDOFF.
		TC	Q2ROOT
## Page 687
		TC	DANZIG
NOROOT		CAF	ZERO		# NEG ARG FROM ROUNDOFF. SET =0.
		TS	MPAC
		TS	MPAC +1
		TC	DANZIG
		
Q2ROOT		TC	BANKCALL	# GO TAKE DP SQRT
		CADR	SQRT3		# C(MPAC +2) =0
		TC	DANZIG
		
NEGQ2		CAF	POSMAX		# THIS WILL LEAVE PUSHLOC AT PDL2
		TS	TFF
		TS	TFF +1
		CS	FIXLOC		# BUT RESET IT.
		COM
		TS	PUSHLOC
		
		TC	INTPRET
		
		ITCQ	0		# AND RETURN TO CALLING PROGRAM.
		
# ENTER WITH (2-RTERM ALFA-RMAG ALFA) 2(-3) IN MPAC,+1. Q1-Q2 IN PDL 8,9

GETTFF		INDEX	FIXLOC
		INDEX	16D		# COUNT BY 1,5
		CAF	BIT4		# EFFECTIVE SL 3  (2 OR 4 OR 5 )
		TC	SHORTMP		# C(MPAC +1,+2) = DEN    ON RETURN.
		INDEX	FIXLOC		# (UNSCALD NUM AND DEN LEQ +/- 2 LCE)
		CS	8D
		XCH	MPAC +1		# SAVE Q2-Q1,  GET DEN
		TS	MPAC		# AND SAVE.
		INDEX	FIXLOC
		INDEX	16D		# COUNT BY 1,5
		CAF	-1/2PI
		EXTEND
		MP	MPAC +1
		TS	MPAC +2		# -(Q2-Q1) /2PI    SAVED
		
		TC	SPARCTAN	# NUM=C(MPAC+1),DEN=C(MPAC)
					# RETURN WITH DEL ECC. ANOM/2PI IN A
					# RANGE (0,1)
		AD	MPAC +2
		XCH	MPAC		# TFF NORM=(DEL E:(Q2:Q1))/2PI
					# FOLLOWING TEST VALID WHEN SPTEM3 L .5
		INDEX	FIXLOC
		XCH	18D		# IF BOTH SGN Q1 AND SGN ATAN ARE
		AD	+2		# NEG, THEN SET TFF, +1  =+0.  (-3/4+1 )
		AD	SGNATAN		# LEFT BY SPARCTAN
		TS	A		# (=50K =-3/4 +1 FOR OVFL TEST) 
		TC	GETTFF3		# OK, GO ON
## Page 688
		CAF	ZERO
		TC	NEGQ2 +1	# GONE PAST. SET TFF=0.
		
GETTFF3		INDEX	FIXLOC
		INDEX	19D		# -N.  BIT4 BELOW VALID FOR N LEQ 10D.
		CAF	BIT4		# SCALE FACTOR,  2(-11+N)
		EXTEND
		MP	MPAC
		TS	MPAC
		XCH	LP
		TS	MPAC +1		# NOW READY TO MULT BY ORB PERIOD
		TC	DANZIG
		
# TO IMPROVE PRECISION OF SPARCTAN AT SMALLER LCE, COME HERE TO GET NORMALIZATION FACTOR
# BASED ON SIZE OF LCE SQ.  SHIFT BY 2,S. SCALING RESULTING FROM BELOW YIELDS AT LEAST ONE LEAD ZERO IN
# NUM AND DEN USED IN SPARCTAN.

NORMBYE		CAF	OCT37440	# BRACKET SIZE BY OVFL TESTS
		AD	MPAC		# C(MPAC) = (1-LCE SQ)  2(-6)
		TS	A
		TC	ENORM0		# HERE IF LCE GEQ 2(-1)
		AD	OCTNEG30	# (EFFECTIVE CONST  37410)
		TS	A
		TC	ENORM1		# HERE IF LCE GEQ 2(-2)
		AD	OCTNEG6		# (EFFECTIVE CONST  37402)
		TS	A
		TC	ENORM2		# HERE IF LCE GEQ 2(-3)
		CS	BIT2		# HERE IF LCE  L  2(-3)
ENORM3		INDEX	FIXLOC
		TS	16D		# COUNT FOR SL BY 1,S ALSO NEEDED
		AD	A
		INDEX	FIXLOC
		TS	17D		# COUNT FOR SL BY 2,S.
		TC	DANZIG
		
ENORM0		CAF	BIT1
		TC	ENORM3
ENORM1		CAF	ZERO
		TC	ENORM3
ENORM2		CS	BIT1
		TC	ENORM3
		
OCT37440	OCT	37440
OCTNEG30	OCT	-00030
OCTNEG6		OCT	-00006

1/MUE		2DEC*	.250876044 E-10 B35*	# CS SQ/M CUBE
MUE(37)		2DEC*	3.986032233 E10 B-37*	# CS SQ/M CUBE
## Page 689
DP2PI/16	2DEC	.39269909
		DEC	-.15915494 B-2
		DEC	-.15915494 B-1
-1/2PI		DEC	-.15915494
		DEC	-.15915494 B1
NOMBURN		OCT	00171
ALFALIM		2DEC	6.562 E-8 B21

## Page 690

# INVERSE TANGENT.     THE SINGLE PRECISION ARCTAN IS BASED ON A HASTINGS  3 TERM POLYNOMIAL.  THE PROGRAM
# ACCEPTS  NUM  AND  DEN  , WHERE  TAN(THETA) = NUM/DEN, AND GIVES  THETA/PI  IN THE RANGE (0,1)
# ENTER WITH C(MPAC +1) = NUM, AND C(MPAC) = DEN.   NOTE THAT FOR CALCTFF, THETA = DEL E/2.
# SPARCTAN USES 3 SP TEMPORARIES:  SPTEMQ, SPTEM1, SPTEM2

# TEMPORARY ERASABLE ASSIGNMENTS FOR SPARCTAN ROUTINE

SPTEMQ		=	VBUF
SPTEM1		=	VBUF +1
SPTEM2		=	VBUF +2
SPATANQ		EQUALS	SPTEMQ
SGNATAN		EQUALS	SPTEM1

SPARCTAN	XCH	Q
		TS	SPATANQ
		
		CAF	BIT13		# 1/4 = PI/4
		TS	SPTEM2
		TS	SGNATAN		# IF NUM=0, THETA=0
		CCS	MPAC +1		# NUM
		TC	TESTDEN		# IF DEN =0, THETA=90 DEG SGN(NUM)
		TC	TESTDEN		# MUST STILL TEST DEN.
		TC	+2		# INDETREMINATE CASE 0/0 CONSIDERED AS 90D
		TC	TESTDEN -1
		CS	SGNATAN
		TS	SGNATAN
		CS	MPAC +1
		TS	MPAC +1		# ABVAL OF NUM.
		
TESTDEN		CCS	MPAC		# DEN
		TC	SPATAN1
		TC	SPATAN4		# 90 DEG CASE
		TC	+2
		TC	SPATAN4		# 90 DEG CASE
		CS	SGNATAN
		TS	SGNATAN
		CS	MPAC
		TS	MPAC		# ABVAL OF DEN.
		
		CCS	MPAC +1		# EITHER POS OR +0. WAS NUM ZERO
		TC	TESTDEN		# NO, GO ON
		TC	SPATANQ		# YES, THETA =0      RETURN
		
SPATAN1		EXTEND			# HERE NEGLECT 1 BIT OF CCS. LOST IN MP.
		MP	ITANC0		# POLY TRANSITION TEST.
		AD	MPAC +1		# NUM  (PNZ, SO +0 IN CCS IMPOSS)
		CCS	A		# IF NEG, USE Z=N/D FOR POLY.
		TC	SPATAN2		# IF POS, USE Z=(N-D)/(N+D) FOR POLY.
ITANC0		DEC	-.2714558	# =-(1-K)/(1+K), AND K=.573
		CAF	ZERO		# THIS FORM OF POLY HAS BETTER BEHAVIOR
## Page 691
		TS	SPTEM2		# NEAR ORIGIN. REPLACE PI/4 BY ZERO.
		XCH	MPAC +1		# SET +0 TO FIT IN WITH CALC BELOW.
		TC	SPATAN3
		
SPATAN2		CS	MPAC		# N-D AND N+D WILL NOT OVFL IN AD IF LCE
					# L 1.0 DUE TO NORMALIZATION BY LCE.
		AD	MPAC +1		# NUM
SPATAN3		TS	SR
		XCH	MPAC
		AD	MPAC +1
		TS	A		# DID IT OVFL..
		TC	SPATAN5		# NO, GO DO DV.
		XCH	SR		# YES, SHIFT BOTH NUM AND DEN R1.
		XCH	SR		# (NUM-DEN)/4 IN SR
		AD	HALF		# (NUM+DEN)/2 IN A.
		
SPATAN5		TS	MPAC		# NORMALLY, (NUM+DEN)
		XCH	SR		# NORMALLY, (NUM-DEN) /2
		EXTEND	
		DV	MPAC		# NO OVFL PROB.
		TS	MPAC		# Z/2= (NUM-DEN)/2(NUM+DEN)
					# Z/2 = NUM/2DEN       NEAR ORIGIN
		
		EXTEND
		SQUARE
		TS	MPAC +1		# Z Z/4
		EXTEND
		MP	ITANC7
		AD	ITANC5
		EXTEND
		MP	MPAC +1
		AD	ITANC3
		EXTEND
		MP	MPAC +1
		AD	ITANC1
		DOUBLE
		EXTEND
		MP	MPAC
		AD	SPTEM2		# EITHER 1/4=PI/4 OR 0
		TS	Q		# C(A)=THETA/PI ,RANGE( 0 ,.5) SEE NOTE
		CCS	SGNATAN
		XCH	Q		# AFFIX SGN FOR POLY TO HAVE RANGE (-.5,.5
		TC	SPATANQ
		CS	Q
		AD	HALF
SPATAN4		AD	HALF		# 90 DEG CASE COME HERE
		TC	SPATANQ		# RETURN WITH THETA/  PI IN A. RANGE(0,1)
					# POLY GIVES THETA/ PI RANGE(-.5,+.5)

# NOTE *** RETURN TO CALCTFF  WITH  DEL E/2 PI IN A, RANGE (0,1).	SINCE THE OUTPUT OF  SPARCTAN ,
## Page 692
# IS IN THE RANGE (0,1), THE +/- 90 DEG CASES ARE IDENTICAL.  FURTHER SINCE  FOR  CALCTFF, THE ANGLE THETA/PI
# EQUALS DEL E/2 PI,  SPARCTAN  CONSIDERS BOTH 0 DEG AND 180 DEG AS 0 DEG.  SHOULD PROGRAMS OTHER THAN  CALCTFF
# DESIRE ARC TANGENTS, THEN  SPARCTAN  CAN BE MADE GENERAL.

ITANC1		DEC	.318060008	# =C1 /PI
ITANC3		DEC	-.40894149	# = 4 C3/PI
ITANC5		DEC	.7449806	# 16 C5/PI
ITANC7		DEC	-.79435682	# 64 C5 /PI					74W

## Page 693

HIECC1		BMN	3
		LXC,1	LODON
		DMOVE	TSRT*
		RTB
			14D
			TFFMAX
			19D
			12D
			11D,1
			FRESHPD
		STORE	TFF
		
		ITCQ	0
		
TFFMAX		DMOVE	1
		RTB
			NEARONE
			FRESHPD
		STORE	TFF
		
		ITCQ	0
		
HIECC		DMOVE	1
		RTB
			10D
			FRESHPD
		STORE	14D
		
		VXV	1
		ABVAL	TSLT
			RN
			VN
			2
		
		NOLOD	1
		DMP	TSLT
			1/MUE
			1		# H/MUE  B5  PD2,3
		
		NOLOD	1
		DMP
			0		#  H H/MUE  B-26  PD4,5
		UNIT	2
		DOT	DMP
		TSLT	DSQ
			RN
			VN
			2
			2
		DDV	2
## Page 694
		DSU	DSQ
		DAD	SQRT
			4D
			RMAG
			TWO(-2)		# E  B-1  PD6,7
		
		NOLOD	0
		STORE	ECC
		
		NOLOD	1
		BDSU	TSRT
			TWO(-2)
			1		# B-2    DEL    PD8,9
		
		TSRT	0
			4
			1		# PD10,11  B-27
		
		DSU	1
		BDDV
			TWO(-2)
			8D		# H H/MUE(2-DEL)  B-25  =RPAR  PD10,11
		
		TSRT	1
		BDSU	BPL
			R400K
			1
			10D
			TFFMAX
		
		DOT	1
		BPL
			RN
			VN
			HIECC1
		
		DDV	2
		DSU	TSRT
		DDV
			4D
			RMAG
			TWO(-2)
			1
			6D		# COSTH  PD12,13  B-1
		
		NOLOD	1
		BMN	BDSU
			TFFMAX
			TWO(-2)
## Page 695
		DAD	2
		BDDV	SQRT
		COMP
			TWO(-2)
			12D		# PD14,15 B0  TAN(TH/2)
		
		TSRT	2
		BDDV	DSU
		TSRT	DDV
			R400K
			1
			4D
			TWO(-2)
			1
			6D
		
		NOLOD	1
		BMN	BDSU
			TFFMAX
			TWO(-2)
		
		DAD	2
		BDDV	SQRT
		COMP
			TWO(-2)
			16D		# TAN(TH/2)  PD18,19
		
		TSRT	1
		DAD
			8D
			1
			TWO(-2)
		
		DSU	1
		BMN	DMP
			18D
			14D
			TFFQ		# (1+DEL/4)(TANTH1/2)-TAN(TH/2))
		
		DSQ	1
		DMP
			18D
			18D		# (TAN(TH1/2))CUBED  PD22,23
		
		DSQ	1
		DMP	BDSU
			14D
			14D		# PD22,23   B0
		
		TSRT	2
## Page 696
		BDSU	DMP
		DAD
			8D
			1
			DP1/3S
			-
			20D
		
		DMP	3
		TSLT	SQRT
		DMP	DMP
		TSRT	RTB
			10D
			1/MUE
			1
			10D
			22D
			7D
			FRESHPD
		STORE	TFF
		
		ITCQ	0
		
TFF0		RMOVE	1
		RTB
			ZERODP
			FRESHPD
		STORE	TFF
		
		ITCQ	0
		
		ITCQ	0
		
TWO(-2)		2DEC	.5
DP1/3S		2DEC	.3333333333 B-1
